	import {
		baseUrl
	} from '@/api/base.js';
let HTTP_REQUEST_URL=baseUrl;
export default {

	customDateStringToTimestamp(str) {
		const [year, month, day, hour, minute, second] = str.split(/[- :]/);
		console.log(year, month - 1, day, hour, minute, second);
		return new Date(year, month - 1, day, hour || '00', minute || '00', second || '00').getTime(); // 注意月份减1
	},

	appPay(type, data, callback_succ_func, callback_error_func) {
		
		uni.requestPayment({
			provider: type,
			orderInfo:data, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
			success: function(res) {
				console.log('success:' + JSON.stringify(res));
				if (callback_succ_func) {
					callback_succ_func(res);
				}
			},
			fail: function(err) {
				console.log('fail:' + JSON.stringify(err));
				if (callback_error_func) {
					callback_error_func(err);
				}
			}
		});
	},

	// 微信支付 h5
	jsApiCall(data, callback_succ_func, callback_error_func) {
		//使用原生的，避免初始化appid问题  
		WeixinJSBridge.invoke('getBrandWCPayRequest', {
				appId: data['appId'],
				timeStamp: data['timeStamp'],
				nonceStr: data['nonceStr'],
				package: data['package'],
				signType: data['signType'],
				paySign: data['paySign']
			},
			function(res) {
				var msg = res.err_msg ? res.err_msg : res.errMsg;
				//WeixinJSBridge.log(msg);  
				switch (msg) {
					case 'get_brand_wcpay_request:ok': //支付成功时  
						if (callback_succ_func) {
							callback_succ_func(res);
						}
						break;
					default: //支付失败时  
						// WeixinJSBridge.log('支付失败!' + msg + ',请返回重试.');
						if (callback_error_func) {
							callback_error_func({
								msg: msg
							});
						}
						break;
				}
			})
	},

	/*
	 * 单图上传
	 * @param object opt
	 * @param callable successCallback 成功执行方法 data 
	 * @param callable errorCallback 失败执行方法 
	 */
	uploadImageOne: function(opt, successCallback, errorCallback) {

		let that = this;
		if (typeof opt === 'string') {
			let url = opt;
			opt = {};
			opt.url = url;
		}
		let count = opt.count || 9,
			sizeType = opt.sizeType || ['compressed'],
			sourceType = opt.sourceType || ['album', 'camera'],
			is_load = opt.is_load || true,
			uploadUrl = opt.url || '',
			inputName = opt.name || 'file',
			fileType = opt.fileType || 'image';
		uni.chooseImage({
			count: count, //最多可以选择的图片总数  
			sizeType: sizeType, // 可以指定是原图还是压缩图，默认二者都有  
			sourceType: sourceType, // 可以指定来源是相册还是相机，默认二者都有  
			success: function(res) {
				
				//启动上传等待中...  
				uni.showLoading({
					title: '上传中',
				});

				uni.uploadFile({
					url: HTTP_REQUEST_URL + '/api/upload/uploadFile',
					filePath: res.tempFilePaths[0],
					fileType: fileType,
					name: inputName,
					formData: {
						'filename': inputName
					},
					success: function(res) {
						uni.hideLoading();
						let data = res.data ? JSON.parse(res.data) : {};
						if (data.code == 200) {
							successCallback && successCallback(data)
						} else {
							errorCallback && errorCallback(data);
							uni.$u.toast(data.msg)
						}
					},
					fail: function(res) {
						uni.$u.toast('上传图片失败')
					}
				})
			}
		})
	},

	uploadVideoOne: function(opt, successCallback, errorCallback) {
		let that = this,
			inputName = opt.name || 'file',
			fileType = opt.fileType || 'video';
		uni.chooseVideo({
			success(res) {


				uni.showLoading({
					title: '上传中',
				});

				uni.uploadFile({
					url: HTTP_REQUEST_URL + '/api/upload/uploadFile',
					filePath: res.tempFilePath,
					fileType: fileType,
					name: inputName,
					formData: {
						'filename': inputName
					},
					success: function(res) {
						uni.hideLoading();
						let data = res.data ? JSON.parse(res.data) : {};
						if (data.code == 200) {
							successCallback && successCallback(data)
						} else {
							errorCallback && errorCallback(data);
							uni.$u.toast(data.msg)
						}
					},
					fail: function(res) {
						uni.$u.toast('上传视频失败')
					}
				})


			}
		})
	},
	
	// 上传图片
	uploadItem(path,fileType,inputName){
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: HTTP_REQUEST_URL + '/api/upload/uploadFile',
				filePath: path,
				fileType: fileType,
				name: inputName,
				formData: {
					'filename': inputName
				},
				success: function(res) {
					// uni.hideLoading();
					let data = res.data ? JSON.parse(res.data) : {};
					if (data.code == 200) {
						// successCallback && successCallback(data)
						resolve(data);
					} else {
						
						uni.$u.toast(data.msg)
					}
				},
				fail: function(res) {
					uni.$u.toast('上传图片失败')
				}
			})
		})
	},
	
	// 多图上传
	uploadImageDuo(opt, successCallback, errorCallback) {
		let that = this;
		if (typeof opt === 'string') {
			let url = opt;
			opt = {};
			opt.url = url;
		}
		let count = opt.count || 9,
			sizeType = opt.sizeType || ['compressed'],
			sourceType = opt.sourceType || ['album', 'camera'],
			is_load = opt.is_load || true,
			uploadUrl = opt.url || '',
			inputName = opt.name || 'file',
			fileType = opt.fileType || 'image';
		uni.chooseImage({
			count: count, //最多可以选择的图片总数  
			sizeType: sizeType, // 可以指定是原图还是压缩图，默认二者都有  
			sourceType: sourceType, // 可以指定来源是相册还是相机，默认二者都有  
			success:  async function(res) {
				
				//启动上传等待中...  
				uni.showLoading({
					title: '上传中',
				});
				let imgArr = []
				// console.log( res.tempFilePaths);
				for(let i = 0;i < res.tempFilePaths.length; i++){
					console.log(i,res.tempFilePaths.length);
					let imgfile = await that.uploadItem(res.tempFilePaths[i],fileType,inputName);
					imgArr.push(imgfile)
				}
				
				uni.hideLoading();
				uni.$u.toast('上传成功')
				successCallback && successCallback(imgArr)
				
			}
		})
	},
}

	